﻿using WT.OpenIddict.Repository.Seed;

namespace WT.OpenIddict.WebApi;

public class Worker : IHostedService
{
    private readonly ApplicationProvider _applicationProvider;
    private readonly IServiceProvider _serviceProvider;


    private readonly UserRoleSeedProvider _userRoleSeedProvider;


    public Worker(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
        _applicationProvider = _serviceProvider.GetRequiredService<ApplicationProvider>();
        _userRoleSeedProvider = _serviceProvider.GetRequiredService<UserRoleSeedProvider>();
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        await _applicationProvider.InitSeedData();
        await _userRoleSeedProvider.InitSeedData();
        await Console.Out.WriteLineAsync("种子数据加载完毕");
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        return Task.CompletedTask;
    }
}